package com.itsc.dao;

import java.util.List;

import org.hibernate.Query;

import com.itsc.entities.User;
import com.itsc.util.page.Page;

@SuppressWarnings("unchecked")
public class UserDao extends BaseDao<User> {

	/**
	 * 根据名称查找
	 * 
	 * @param name
	 * @param password
	 * @return
	 */
	public boolean selectByString(String name) {
		String hql = "from User where user_name=?";
		Query query = getSession().createQuery(hql);
		query.setString(0, name);
		List<User> userList = query.list();
		if (userList.size() == 0) {
			return true;
		} else {
			return false;
		}
	}

	public Integer selectIdByString(String name) {
		Integer id = 0;
		String hql = "from User where user_name=?";
		Query query = getSession().createQuery(hql);
		query.setString(0, name);
		List<User> list = query.list();
		for (int i = 0; i < list.size(); i++) {
			id = list.get(0).getUser_id();
		}
		return id;
	}

	public List<User> search(String name, String datemin, String datemax,
			Page page) {
		name = name == null ? "" : name;
		datemin = datemin == null ? "" : datemin;
		datemax = datemax == null ? "" : datemax;
		String hql = "from User where user_regtime between ? and ? or user_name like ?"
				+ "or user_phone like ? or user_email like ? or user_scose=?";
		Query query = getSession().createQuery(hql);
		query.setString(0, datemin);
		query.setString(1, datemax);
		query.setString(2, "%" + name + "%");
		query.setString(3, "%" + name + "%");
		query.setString(4, "%" + name + "%");
		query.setString(5, name);
		if (page != null) {
			query.setFirstResult(page.getBeginIndex());
			query.setMaxResults(page.getPageSize());
		}
		List<User> userList = query.list();
		return userList;
	}

	public boolean checkUserPhone(String phone) {
		
		String hql = "from User where user_phone=?";
		Query query = getSession().createQuery(hql);
		query.setString(0, phone);
		List<User> userList = query.list();
		if (userList.size() == 0) {
			return true;
		} else {
			return false;
		}
	}

	public boolean checkUserEmail(String email) {
		String hql = "from User where user_email=?";
		Query query = getSession().createQuery(hql);
		query.setString(0, email);
		List<User> userList = query.list();
		if (userList.size() == 0) {
			return true;
		} else {
			return false;
		}
	}
	
	public Integer selectIdByPhone(String phone){
		Integer id = 0;
		String hql = "from User where user_phone=?";
		Query query = getSession().createQuery(hql);
		query.setString(0, phone);
		List<User> list = query.list();
		for (int i = 0; i < list.size(); i++) {
			id = list.get(0).getUser_id();
		}
		return id;
	}
	
	public Integer selectIdByEmail(String email){
		Integer id = 0;
		String hql = "from User where user_email=?";
		Query query = getSession().createQuery(hql);
		query.setString(0, email);
		List<User> list = query.list();
		for (int i = 0; i < list.size(); i++) {
			id = list.get(0).getUser_id();
		}
		return id;
	}

}
